import com.jogamp.opengl.*;

public class BasicFrame implements GLEventListener {

    Board board;

    BasicFrame() {
        board = new Board();
    }

    BasicFrame(int height, int width) {
        board = new Board(height, width);
    }

    @Override
    public void init(GLAutoDrawable glAutoDrawable) { }

    @Override
    public void dispose(GLAutoDrawable glAutoDrawable) { }

    @Override
    public void display(GLAutoDrawable glAutoDrawable) {
        final GL2 gl = glAutoDrawable.getGL().getGL2();
        drawRectanglesFromBoard(gl, board);
        board.nextState();
        board.showBoard();
        System.out.println();
    }

    @Override
    public void reshape(GLAutoDrawable glAutoDrawable, int i, int i1, int i2, int i3) { }


    public void drawRectanglesFromBoard(GL2 gl, Board board) {
        float lengthX = 2f / board.width;
        float lengthY = 2f / board.height;
        float leftTopX = -1f, leftTopY = 1f;
        int[][] liveBoard = board.getActiveBoard();
        for (int i = 1; i < board.height + 1; i++) {
            leftTopX = -1f;
            for (int j = 1; j < board.width + 1; j++) {
                drawRectangle(gl, leftTopX, leftTopY, lengthX, lengthY, liveBoard[i][j] == 1);
                leftTopX += lengthX;
            }
            leftTopY -= lengthY;

        }


    }

    public void drawRectangle(GL2 gl, float leftTopX, float leftTopY, float lengthX, float lengthY, boolean alive){
        if (alive) {
            gl.glColor3f(1, 1, 1);
        } else {
            gl.glColor3f(0, 0, 0);
        }
        gl.glBegin(GL2.GL_QUADS);
        gl.glVertex2f(leftTopX, leftTopY);
        gl.glVertex2f(leftTopX + lengthX, leftTopY);
        gl.glVertex2f(leftTopX + lengthX, leftTopY - lengthY);
        gl.glVertex2f(leftTopX, leftTopY - lengthY);
        gl.glEnd();

    }


}
